package com.ruoyi.system.utils;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.OrderTime;
import com.ruoyi.system.domain.JiajiaOrder;
import com.ruoyi.system.domain.JiajiaWorker;
import com.ruoyi.system.domain.JiajiaWorkerJiedan;
import com.ruoyi.system.domain.JiajiaWorkerJiedanDetail;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class OrderUtil {



    //获取最早可约时间
    public static String getZuiZao(JiajiaWorker worker,
                                   List<JiajiaOrder> orderList,
                                   List<JiajiaWorkerJiedan> jiedanList,
                                   List<JiajiaWorkerJiedanDetail> detailList){

        List<OrderTime> orderTimeList =getCanYueList(worker,orderList,jiedanList,detailList);
        OrderTime resOrderTime = new OrderTime();
        resOrderTime.text="暂无";
        for (OrderTime orderTime : orderTimeList) {
            if (orderTime.isUse ){
                resOrderTime = orderTime;
                break;
            }
        }
        return resOrderTime.text;
    }


    //可以预约的时间列表
    public static List<OrderTime> getCanYueList(JiajiaWorker worker,
                                                List<JiajiaOrder> orderList,
                                                List<JiajiaWorkerJiedan> jiedanList,
                                                List<JiajiaWorkerJiedanDetail> detailList){
        if (worker == null)return new ArrayList<>();
        if (orderList == null)orderList = new ArrayList<>();
        if (jiedanList == null)jiedanList = new ArrayList<>();
        if (detailList == null)detailList = new ArrayList<>();


//        int star = Integer.valueOf(worker.getStarTime().split(":")[0]);
//        int end = Integer.valueOf(worker.getEndTime().split(":")[0]);
        int star = 8;
        int end = 23;

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        int currentHour = calendar.get(calendar.HOUR_OF_DAY);

        List<OrderTime> orderTimeList = new ArrayList<>();
        //第一步先 生成未来7天的时间段
        for (int i = 0; i < 6; i++) {
            for (int j = star; j < end; j++) {
                OrderTime orderTime = new OrderTime();
                if (j< currentHour && i == 0){
                    continue;
                }else{

                    if (i==0 && j==currentHour){
                        continue;
                    }else{
                        orderTime.hour = j;
                    }
                }
                orderTime.date = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,DateUtils.getDayBeforOrAfterByToDay(i));
                orderTime.isUse = true;
                String text ="";
                if(i == 0){

                }else if (i==1){
                    text = "明天";
                }else{
                    text = orderTime.date.split("-")[2]+"日";
                }

                orderTime.text = text + (orderTime.hour<=9?"0"+orderTime.hour:orderTime.hour)+":00";
                orderTimeList.add(orderTime);
            }
        }

        //第二部 生成社工休息的时间段数组
        for (JiajiaWorkerJiedan model : jiedanList) {
            if (model.getStatus().intValue() == 0){//0休息 1上班
                for (OrderTime orderTime : orderTimeList) {
                    if (orderTime.date.equals(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,model.getDay()))){
                        orderTime.isUse = false;
                    }
                }
            }
            if (model.getStatus().intValue() == 1){//0休息 1上班
                for (JiajiaWorkerJiedanDetail detail:detailList) {
                    if (model.getId().longValue() == detail.getJdid().longValue()){
                        for (OrderTime orderTime : orderTimeList) {
                            if (orderTime.date.equals(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,model.getDay()))){
                                int tempStar = Integer.valueOf(detail.getStartime().split(":")[0]);
                                if (orderTime.hour == tempStar){
                                    orderTime.isUse = false;
                                }
                            }
                        }
                    }
                }
            }
        }
        //第三部 生成下单时间段的数组
        for (JiajiaOrder order : orderList) {
            Calendar calendar1 = Calendar.getInstance();
            calendar1.setTime(new Date());
            //项目时间倒推4个小时
            calendar1.add(Calendar.HOUR_OF_DAY,-3);
            Calendar starC = calendar1;

            //项目时间 + 1；

            //int a = (int)Math.ceil(order.getTimeSpan().doubleValue()/60.f);
            int a = order.getTimeSpan();
            calendar1 = Calendar.getInstance();
            calendar1.setTime(new Date());
            calendar1.add(Calendar.HOUR_OF_DAY,a+1);
            Calendar endC = calendar1;

            while(starC.before(endC))
            {
                String date = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,starC.getTime());
                int chour = starC.get(Calendar.HOUR_OF_DAY);
                for (OrderTime orderTime : orderTimeList) {
                    if (orderTime.date.equals(date)){
                        if (orderTime.hour == chour){
                            orderTime.isUse = false;
                        }
                    }
                }
                starC.add(Calendar.HOUR_OF_DAY,1);
            }
        }
        return orderTimeList;
    }

}


